#include "stack.h"
#include<stdlib.h>



void stack_test(Stack *pstack)
{
	if(pstack->size == pstack->max_size)
	{
		Pointer *t = (Pointer *)malloc((pstack->max_size+ pstack->increment)*sizeof(Pointer));
		size_t i;
		for(i=1;i<pstack->max_size;i++)
		{
			t[i]=pstack->data[i];
		}
		free(pstack->data);
		*pstack->data = *t;
		pstack->max_size = pstack->max_size + pstack->increment;
	}
}

void stack_create(Stack *pstack)
{
	pstack->initial_size=50;
	pstack->increment=50;
	pstack->data = (Pointer *)malloc(pstack->initial_size+1*sizeof(Pointer));
	pstack->size=0;
	pstack->max_size=pstack->initial_size;
}

void stack_destroy(Stack *pstack)
{
	free(pstack->data);
	pstack->size=0;
	pstack->max_size=0;
}

void stack_push(Stack *pstack, Pointer value)
{
	stack_test(pstack);

	pstack->data[pstack->size] = value;

	pstack->size++;
}

Pointer stack_pop(Stack *pstack)
{
	if(pstack->size == 0)
		return(0);
	pstack->size--;
	return(pstack->data[pstack->size]);
}

Pointer stack_peek(Stack *pstack)
{
	if(pstack->size == 0)
		return(0);
	return(pstack->data[pstack->size-1]);
}

size_t stack_size(Stack *pstack)
{
	if(pstack->size == 0)
		return(0);
	return(pstack->size);
}




void stack_tune(Stack *pstack, size_t initial_size, size_t increment)
{
	pstack->initial_size=initial_size;
	pstack->increment=increment;
}